package com.vfa.fishing.data;
import java.io.IOException;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteException;
import android.util.Log;
public class GameModel {
     private DBAdapter myDAdapter;

        public GameModel(Context context) {
            myDAdapter = new DBAdapter(context);
            create();
        }

        private void create() {
            try {
                myDAdapter.createDataBase();
            } catch (IOException ioe) {
                Log.e("LocNNT ",
                        "GameModel   Unable to create database  =============");
            }
            try {
                myDAdapter.openDataBase();
            } catch (SQLException sqle) {
                Log.e("LocNNT ", "GameModel   Unable to Open database  =============");
            }

        }
        public void Insert(int level, int score ,int time) { // , boolean isMoving,boolean
                                                // isTimeDelayInFishing) {
            if (!myDAdapter.myDataBase.isOpen()) {
                myDAdapter.openDataBase();
            }
            String sql = "INSERT INTO tbl_gameInfo(i_level,i_score,time_percent) VALUES ("
                    + level
                    + ",'" + score+ "','"+time+ "')";
            myDAdapter.execSQL(sql);
        }

        public ArrayList<MainGame>getDataList() {

            if (!myDAdapter.myDataBase.isOpen()) {
                myDAdapter.openDataBase();
            }
//            PersonToRestore lst = new PersonToRestore();
            ArrayList<MainGame> lst = new ArrayList<MainGame>();
            Cursor result = null;
            String sql = "SELECT * FROM tbl_gameInfo ";

            result = myDAdapter.Query(sql);
            if (result == null) {
                myDAdapter.close();
                return null;
            }
            if (result.moveToFirst()) {
                do {
                    try {

                        MainGame objDB = new MainGame();
                        objDB.setLevel(result.getInt(0));
                        objDB.setScore(result.getInt(1));
                        objDB.setTimePercent(result.getInt(2));
                        lst.add(objDB);
                    } catch (SQLiteException ex) {
                        Log
                                .d("Data", "Error when load recipe data"
                                        + ex.toString());
                    }

                } while (result.moveToNext());
            }

            if (result != null && !result.isClosed()) {
                result.close();
            }

            myDAdapter.close();
            return lst;
        }

        public void deleteAll() {
            if (!myDAdapter.myDataBase.isOpen()) {
                myDAdapter.openDataBase();
            }
            String DELETE = "delete from tbl_gameInfo";
            myDAdapter.myDataBase.execSQL(DELETE);
        }


}
